﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace InstrumentationLibrary.Config
{
    /// <summary>
    /// Instrumentation configuration
    /// </summary>
    public class InstrumentationConfiguration : ConfigurationSection
    {
        private const string sectionName = "instrumentation";
        private const string aspectCatalog = "aspectCatalog";

        private static InstrumentationConfiguration instance;

        private InstrumentationConfiguration()
        {
        }

        /// <summary>
        /// Collection of aspects that make up the Aspect Catalog
        /// </summary>
        [ConfigurationCollection(typeof(AspectElementCollection), AddItemName = "aspect")]
        [ConfigurationProperty(aspectCatalog, IsRequired=true)]
        public AspectElementCollection AspectCatalog
        {
            get
            {
                return this[aspectCatalog] as AspectElementCollection;
            }
            set
            {
                this[aspectCatalog] = value;
            }
        }

        /// <summary>
        /// Get's an instance of <see cref="InstrumentationConfiguration"/>
        /// </summary>
        public static InstrumentationConfiguration Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = ConfigurationManager.GetSection(sectionName) as InstrumentationConfiguration;

                    if (instance == null)
                    {
                        throw new 
                            InvalidOperationException("Configuration section 'instrumentation' is not of type 'InstrumentationConfiguration'");
                    }
                }

                return instance;
            }
        }
    }
}
